 /*
 * Copyright (C) 2014 ilknur
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package terp.users;

import terp.data.model.Employee;
import terp.data.dao.EmployeeDao;

/**
 *
 * @author ilknur
 */


public final class UserUtil {
    
    // Local variable
    private static String _user = null;
    private static long _groupId = -1;
    private static boolean _isLogged = false;
    private static boolean _isAdministrator = false;
    
    
    /**
     * return if already a _user is logged
     * @return 
     */
    public static boolean isLogged(){
        return UserUtil._isLogged;
    }
    
    /**
     * get current _user name
     * @return 
     */
    public static String currentUser(){
        return UserUtil._user;
    }
    
    /**
     * check if given _user is current _user
     * @param userName
     * @return 
     */
    public static boolean CheckUser(String userName){
        
        // check _user
        if(UserUtil._user.equals(userName) && UserUtil._isLogged)
            return true;
        
        //user not logged
        return false;
    }
    
    /**
     * login _user
     * @param userName
     * @param password 
     */
    public static void LoginUser(String userName, String password){
        
        //if _user already logged then return
        if(UserUtil._isLogged == true && UserUtil._user.equals(userName))
            return;
        
        //get _user
        EmployeeDao db = new EmployeeDao();
        String _sql = "from Employee e "
                + "where e.userName = '" + userName + "' and "
                + "e.type = 1";
        Employee _emp = db.firstOrDefault(_sql);
        
        if(_emp == null)
            return;
        
        if(_emp.getUserName().equals(userName) &&
                _emp.getPassword().equals(password)){
            UserUtil._user = userName;
            UserUtil._isLogged = true;
            UserUtil._groupId = _emp.getGroup().getRowid();
            UserUtil._isAdministrator = (_emp.getGroup().getGroupName()
                    .equals("Administrators"));
        } else {
            UserUtil._user = null;
            UserUtil._isLogged = false;
            UserUtil._groupId = -1;
            UserUtil._isAdministrator = false;
        }          
    }

    public static long getGroupId() {
        return UserUtil._groupId;
    }

    public static boolean isAdministrators() {
        return UserUtil._isAdministrator;
    }
}
